/**
 * Created by bai on 2015/7/17.
 */
var OccupyTable = function(){
    var DELETEORFREECPLIST = {};
    var PAYWAY='cash';
    var COUPON=[];
    var MEMBERID = "";//会员结账，会员的id
    var ORDERID = "";//会员结账，订单的id
    var TOKENID = "";//会员结账，优惠卷id
    var MONEYMEMBER = 0;//会员结账，应付
    /*
     * 改变表头的信息
     * */
    var changeHeaderTitle = function(name){
        $('.header .headerTitle').html(name);
    }
    /************就餐开台的流程开始*******************/
    /*
     * 占用桌的流程开始
     * */
    var occupyFlowStart = function(obj,fun){
        var oId = obj.data('orderid');
        showOccupyTable(oId,fun);
    };
    /*
     * 就餐中的桌子显示信息
     * */
    var showOccupyTable = function(oId,fun){
        var parentsNode =  $('#subchangeOneSeatPage');
        var freeCp="";
        var crossCp="";
        var cpIsNotPrinter = false;
        var cpHuaCp = "active";
        OrderingSystem.getOrderById(oId, function (data) {
            parentsNode.data('title',data.data[0].targetLabel);
            fun();
            //changeHeaderTitle(obj.data('tablename'));

            var dataPort=data.data[0];
            var cpList="";
            var contactWay = (dataPort.orderType=="reserve")?dataPort.reserve.ResMobile:"无";
            $.each(data.data[0].itemList,function(k,v){
                freeCp=v.isFree?"已免单":"";
                crossCp=(v.status=="finished")?"已划菜":"";
                if(v.count > 0){
                    var count = v.count;
                    $.each(data.data[0].itemList,function(k2,v2){
                        if(v2.count < 0){
                            if(v.id == v2.fid){
                                count += v2.count*1;
                            }
                        }
                    });
                    if(count > 0){
                        cpList+='<p class="dish">'+v.name+'('+count+freeCp+crossCp+')</p>'
                    }
                }
            });
            $.each(data.data[0].itemList,function(k,v){
                if(v.status=="confirmed"){
                    if(v.isFree){

                    }else{
                        cpIsNotPrinter = true;
                    }

                }
            });
            if(cpIsNotPrinter){
                cpHuaCp = "active";
            }else{
                cpHuaCp = "active ";
            }
            $('#subchangeOneSeatPage').html('');
            if(dataPort.orderType=='normal'||dataPort.orderType==null){
                $('<ul class="cont">\
                       <li class="stateInfo">\
                           <p class="state">状态</p>\
                           <p class="yeah">就餐中</p>\
                       </li>\
                       <li class="noPersonInfo">\
                           <p class="contactWay">联系方式</p>\
                           <p class="noPersonInfo">无</p>\
                       </li>\
                       <div class="orderAndPay">\
                           <div class="billInfo">\
                               <p class="bill">账单</p>\
                               <div class="yeah">\
                               '+cpList+'\
                               </div>\
                               <p class="goToSee" data-function="btn_orderingFood_occupyTable_Cp" data-id="'+oId+'">〉</p>\
                           </div>\
                       </div>\
                       <li class="sumInfo">\
                           <p class="sum">合计</p>\
                           <p class="money">'+dataPort.total/100+'元</p>\
                       </li>\
                   </div>\
                   <li class="payInfo">\
                       <p class="payState">支付状态</p>\
                       <p class="yeahOrNo">未支付</p>\
                   </li>\
               </ul>\
               <div class="bottom">\
                   <div class="changeOneSeat active" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'">换台</p>\
                   </div>\
                   <div class="addDishes active" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'">加菜</p>\
                   </div>\
                   <div class="cutDishes '+cpHuaCp+'" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'">划菜</p>\
                   </div>\
                   <div class="delDishes active" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'">退菜</p>\
                   </div>\
                   <div class="freeBill active" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'">免单</p>\
                   </div>\
                   <div class="payBill active " data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'">结账</p>\
                   </div>\
               </div>').appendTo('#subchangeOneSeatPage')
            }else{
                $('<ul class="cont">\
                <li class="stateInfo">\
                    <p class="state">状态</p>\
                    <p class="yeah">就餐中</p>\
                </li>\
                <li class="havePersonInfo">\
                <p class="contactWay">联系方式</p>\
                <p class="name">姓名：'+dataPort.reserve.name+'</p>\
            <p class="phone">电话：<span class="phoneNum">'+dataPort.reserve.ResMobile+'</span></p>\
            <div class="phoneIco"></div>\
                </li>\
                    <div class="orderAndPay">\
                        <div class="billInfo">\
                            <p class="bill">账单</p>\
                            <div class="yeah">\
                               '+cpList+'\
                            </div>\
                            <p class="goToSee" data-function="btn_orderingFood_occupyTable_Cp" data-id="'+oId+'">〉</p>\
                        </div>\
                        <li class="sumInfo">\
                            <p class="sum">合计</p>\
                            <p class="money">'+dataPort.total/100+'元</p>\
                        </li>\
                    </div>\
                    <li class="payInfo">\
                        <p class="payState">支付状态</p>\
                        <p class="yeahOrNo">已支付</p>\
                    </li>\
                </ul>\
                <div class="bottom">\
                <div class="changeOneSeat active" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'">\
                    <div class="ico" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'"></div>\
                    <p class="true" data-function="btn_orderingFood_occupyTable_changeTable" data-id="'+dataPort.id+'">换台</p>\
                </div>\
                <div class="addDishes active" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'">\
                <div class="ico" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'"></div>\
                <p class="true" data-function="btn_orderingFood_occupyTable_addCp" data-id="'+dataPort.id+'">加菜</p>\
                </div>\
                    <div class="cutDishes active" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'">\
                        <div class="ico" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'"></div>\
                        <p class="true" data-function="btn_orderingFood_occupyTable_elideCp" data-id="'+dataPort.id+'">划菜</p>\
                    </div>\
                     <div class="delDishes active" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_deleteCp" data-id="'+dataPort.id+'">退菜</p>\
                   </div>\
                   <div class="freeBill active" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'">\
                       <div class="ico" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'"></div>\
                       <p class="true" data-function="btn_orderingFood_occupyTable_freeCp" data-id="'+dataPort.id+'">免单</p>\
                   </div>\
                    <div class="payBill active " data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'">\
                        <div class="ico" data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'"></div>\
                        <p class="true" data-function="btn_orderingFood_occupyTable_billOrder" data-id="'+dataPort.id+'">结账</p>\
                    </div>\
                </div>').appendTo('#subchangeOneSeatPage')
            }

        });
    };
    /*
     * 占用桌，主页面的菜单详情
     * */
    var occupyTableCpInfo = function(obj,fun){
        var freeCp="";
        var crossCp="";
        var parentsNode = $('#subBillDetailsPage');
        var oId = obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            $('.noPayList',parentsNode).html("");
            $.each(data.data[0].itemList,function(kdata,vdata){
                freeCp=vdata.isFree?"已免单":"";
                crossCp=(vdata.status=="finished")?"已划菜":"";
                var info = "";
                if(freeCp != "" || crossCp != ""){
                    info = "(" + freeCp+crossCp + ")";
                }
                if(vdata.count >0){
                    $('<li class="noPay">\
                    <p class="name">'+vdata.name+'</p>\
                    <p class="count">'+vdata.count+'份'+info+'</p>\
                    <p class="price">￥'+vdata.price/100+'</p>\
                </li>').appendTo('#subBillDetailsPage .noPayList');
                    $.each(data.data[0].itemList,function(kdata2,vdata2){
                        if(vdata2.count < 0 && vdata2.fid == vdata.id){
                            $('<li class="noPay">\
                               <p class="name">'+vdata2.name+'</p>\
                               <p class="count deleteCp">退'+(vdata2.count)*(-1)+'份</p>\
                           </li>').appendTo('#subBillDetailsPage .noPayList');
                        }
                    });
                }
            });
            fun();
        });
    };
    /*
     * 占用桌换台
     * */
    var occupyTableExchangeTable = function(obj,fun){
        var orderId = obj.data('id');
        OrderingSystem.getTableList(function (data) {
            fun();
            $('.chooseSeatIndexPage').hide();
            $('.chooseSeatIndexMain').hide();
            var capital = ['','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾']
            $('.ul-table-list').html('');
            $('<div class="listTitle">\
                <h2>餐台列表</h2>\
                </div>').appendTo('#suborderingFoodchooseSeatPage .ul-table-list')
            $.each(data.data,function(k,v){
                if(v.tableStatus=="OPEN"){
                    var number=(v.tableCap<11)?capital[v.tableCap]:"拾";
                    $('<li data-function="btn_orderingFood_occupyTable_tableList_changeTable" data-id="'+v.id+'" data-orderid="'+orderId+'"> \
                   <div data-function="btn_orderingFood_occupyTable_tableList_changeTable" data-id="'+v.id+'" data-orderid="'+orderId+'">'+number+'</div>\
                   <p data-function="btn_orderingFood_occupyTable_tableList_changeTable" data-id="'+v.id+'" data-orderid="'+orderId+'">'+v.label+'（空） '+v.tableClassName+'</p>\
                   </li>').appendTo('#suborderingFoodchooseSeatPage .ul-table-list');
                }

            })
        });

    };
    /*
     * 占用桌换台，点击桌台列表
     * */
    var occupyTableChangeTableClickTableList = function(obj,fun){
        var tableId=obj.data('id');
        var orderId = obj.data('orderid');
        var oId = orderId;
        var newTId = tableId;
        OrderingSystem.exChangeTable(oId, newTId, function (data) {
            fun();
        });

    }
    /*
     * 占用桌加菜
     * */
    var occupyTableAddCp = function(obj,fun){

        fun();
    };
    /*
     * 占用桌划菜
     * */
    var occupyTableElideCp = function(obj,fun){
        var ulList=$('#subcutDishesPage .cont');
        var oId = obj.data('id');
        var cpIsNotPrinter = true;
        ulList.html('');
        OrderingSystem.getOrderById(oId, function (data) {
            ulList.html('');
            fun();
            $('<li class="tableNum">'+data.data[0].targetLabel+'</li>').appendTo(ulList);
            OrderingSystem.getOrderById(oId, function (data) {
                $.each(data.data[0].itemList,function(k,v){
                    if(v.count > 0){
                        var count = v.count;
                        $.each(data.data[0].itemList,function(k2,v2){
                            if(v2.count < 0 && v.id == v2.fid){
                                count += v2.count*1;
                            }
                        });
                        if(count > 0){
                            if(v.status=='confirmed'){
                                if(v.isFree){//免单的菜不能划菜

                                }else{
                                    $('<li class="dishInfo">\
                    <p class="dishName">'+v.name+'</p>\
                    <p class="dishCount">'+count+'份</p>\
                    <div class="state " data-function="" data-id="'+v.id+'">\
                    <p class="noChoose choosed" data-function="btn_orderingFood_crossFood">√</p>\
                    </div>\
                    </li>').appendTo(ulList);
                                    cpIsNotPrinter = false;
                                }
                            }else if(v.status=="finished"){
                                $('<li class="dishInfo">\
                    <p class="dishName">'+v.name+'</p>\
                    <p class="dishCount">'+count+'份已划菜</p>\
                    <div class="state " style="display: none" data-function="" data-id="'+v.id+'">\
                    <p class="noChoose choosed" data-function="btn_orderingFood_crossFood" style="display: none">√</p>\
                    </div>\
                    </li>').appendTo(ulList);
                            }else{
                                $('<li class="dishInfo">\
                    <p class="dishName" style="  width: 120px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">'+v.name+'</p>\
                    <p class="dishCount" >未打印不能划菜</p>\
                    <div class="state " style="display: none" data-function="" data-id="'+v.id+'">\
                    <p class="noChoose choosed" data-function="btn_orderingFood_crossFood" style="display: none">√</p>\
                    </div>\
                    </li>').appendTo(ulList);
                            }
                        }
                    }
                });
                $('<li class="elide" data-function="btn_orderingFood_elideCp_ok">确认划菜</li>').appendTo(ulList);
            });
            /*     if(cpIsNotPrinter){
             $.each(data.data[0].itemList,function(k,v){
             if(v.status=="finished"){
             alert(1)
             $('<p class="dishName">菜品已经划完</p>').appendTo(ulList);
             }
             })
             }*/
        });

    };
    /*
     * 占用桌退菜
     * */
    var occupyTableDeleteCp = function(obj,fun){
        $('#subdelDishesPage .chooseAllInfo').find("div.chooseIco").removeClass("active");
        $('#subdelDishesPage .bottom').html('');
        $('<div class="true" data-function="btn_orderingFood_deleteCp_comfirm" data-id="'+obj.data('id')+'"> 确定</div>').appendTo('#subdelDishesPage .bottom');
        $('#subdelDishesPage .reasonOptions').html('');
        $('<span class="notHealthy " >顾客不要了</span>\
             <span class="notAll ">顾客误点</span>\
             <span class="loseBill ">厨房没有原材料了</span>\
             <span class="other " >其他</span>').appendTo('#subdelDishesPage .reasonOptions');
        var oId = obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            $('#subdelDishesPage .freeDishesList').html('');
            DELETEORFREECPLIST = {};
            $.each(data.data[0].itemList,function(k,v){
                if(v.isFree){

                }else{
                    if(v.count > 0){
                        var count = v.count;
                        $.each(data.data[0].itemList,function(k2,v2){
                            if(v2.count < 0 && v.id == v2.fid){
                                count += v2.count*1;
                            }
                        });
                        if(count > 0){
                            DELETEORFREECPLIST[v.id] = {count:count};
                            $('<li class="dishInfo" data-id="'+v.id+'">\
                           <div class="chooseIco" data-function="btn_orderingFood_deleteOrFree_oneChoose"></div>\
                           <p class="name">'+v.name+'</p>\
                           <div class="cutIco active" data-function="btn_orderingFood_deleteOrFree_subtract"></div>\
                           <p class="count">'+count+'份</p>\
                           <div class="addIco active" data-function="btn_orderingFood_deleteOrFree_add"></div>\
                           <p class="price">￥'+(v.price/100).toFixed(2)+'</p>\
                       </li>').appendTo('#subdelDishesPage .freeDishesList');
                        }
                    }
                }
            });
            fun();
        });
    };
    /*
     * 占用桌免单
     * */
    var occupyTableFreeCp = function(obj,fun){
        $('#subfreeBillPage .chooseAllInfo').find("div.chooseIco").removeClass("active");
        var oId = obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            $('#subfreeBillPage .bottom').html('');
            $('<div class="true" data-function="btn_orderingFood_freeCp_comfirm" data-id="'+oId+'"> 确定</div>').appendTo('#subfreeBillPage .bottom');
            $('#subfreeBillPage .reasonOptions').html('');
            $('<span class="notHealthy ">顾客不要了</span>\
             <span class="notAll ">顾客误点</span>\
             <span class="loseBill ">厨房没有原材料了</span>\
             <span class="other ">其他</span>').appendTo('#subfreeBillPage .reasonOptions');
            $('#subfreeBillPage .freeDishesList').html('');
            $.each(data.data[0].itemList,function(k,v){
                if(v.isFree){

                }else{
                    if(v.count > 0){
                        var a = true;
                        $.each(data.data[0].itemList,function(k2,v2){
                            if(v2.count < 0 && v2.fid == v.id){
                                a = false;
                            }
                        });
                        if(a){
                            $('<li class="dishInfo" data-id="'+v.id+'">\
                           <div class="chooseIco" data-function="btn_orderingFood_Free_oneChoose"></div>\
                           <p class="name">'+v.name+'</p>\
                           <p class="count">'+v.count+'份</p>\
                           <p class="price"><del>￥'+(v.price/100).toFixed(2)+'</del></p>\
                      </li>').appendTo('#subfreeBillPage .freeDishesList');
                        }
                    }
                }
            });
            fun();
        });

    };
    /*
     *退菜页面的全选按钮
     * */
    var allChoose = function(obj){
        if(obj.hasClass('active')){
            obj.removeClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.chooseIco').removeClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.dishInfo').removeClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.cutIco').addClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.addIco').addClass('active');
        }else{
            obj.addClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.chooseIco').addClass('active');
            obj.parents('.cont').find('.freeDishesList').find('.dishInfo').addClass('active');
            $.each(DELETEORFREECPLIST,function(k,v){
                $('#subdelDishesPage .dishInfo').each(function(){
                    if($(this).data('id') == k){
                        if(v.count > $(this).find('.count').html().replace("份","")*1){
                            if($(this).find('.count').html().replace("份","")*1 == 0){
                                $(this).find('.cutIco').addClass('active');
                                $(this).find('.addIco').removeClass('active');
                            }else{
                                $(this).find('.cutIco').removeClass('active');
                                $(this).find('.addIco').removeClass('active');
                            }
                        }else{
                            $(this).find('.cutIco').removeClass('active');
                            $(this).find('.addIco').addClass('active');
                        }
                    }
                });
            });
        }
    };
    /*
     *退菜页面的每道菜的选择按钮
     * */
    var oneChoose = function(obj){
        if(obj.hasClass('active')){
            obj.removeClass('active');
            obj.parents('.dishInfo').removeClass('active');
            obj.next().next().addClass('active');
            obj.next().next().next().next().addClass('active');
            $('#subdelDishesPage .chooseAllInfo').find('.chooseIco').removeClass('active');
        }else{
            obj.addClass('active');
            obj.parents('.dishInfo').addClass('active');
            if(DELETEORFREECPLIST[obj.parents('.dishInfo').data('id')].count > obj.next().next().next().html().replace("份","")*1){
                if(obj.next().next().next().html().replace("份","")*1 == 0){
                    obj.next().next().addClass('active');
                    obj.next().next().next().next().removeClass('active');
                }else{
                    obj.next().next().removeClass('active');
                    obj.next().next().next().next().removeClass('active');
                }
            }else{
                obj.next().next().removeClass('active');
                obj.next().next().next().next().addClass('active');
            }
            var a = true;
            $('#subdelDishesPage .dishInfo').each(function(){
                if(!$(this).find('.chooseIco').hasClass('active')){
                    a = false;
                }
            });
            if(a){
                $('#subdelDishesPage .chooseAllInfo').find('.chooseIco').addClass('active');
            }
        }
    };
    /*
     *退菜页面中的减号按钮
     * */
    var subtractCpCount = function(obj){
        if(!obj.hasClass('active')){
            var count = obj.next().html().replace("份","")*1;
            if(count > 0){
                obj.next().html(--count+'份');
                obj.next().next().removeClass('active');
                if(count == 0){
                    obj.addClass('active');
                    obj.next().next().addClass('active');
                    obj.parents('.dishInfo').removeClass('active');
                    obj.prev().prev().removeClass('active');
                    $('#subdelDishesPage .chooseAllInfo').find('.chooseIco').removeClass('active');
                }
            }
        }
    };
    /*
     *退菜页面中的加号按钮
     * */
    var addCpCount = function(obj){
        if(!obj.hasClass('active')){
            var count = obj.prev().html().replace("份","")*1;
            if(count < DELETEORFREECPLIST[obj.parents('.dishInfo').data('id')].count){
                obj.prev().html(++count + '份');
                obj.prev().prev().removeClass('active');
                if(count == DELETEORFREECPLIST[obj.parents('.dishInfo').data('id')].count){
                    obj.addClass('active');
                }
            }
        }
    };
    /*
     * 占用桌结账
     * */
    var occupyTableBillOrder = function(obj,fun){
        var oId = obj.data('id');

        OrderingSystem.getOrderById(oId, function (data) {

            $('#subchoosePayWayPage').html('');

            var dataPort=data.data[0];
            var cpList="";
            var timeEat=new Date(dataPort.time.sec*1000);
            var timeNow = new Date();
            timeEat=timeEat.getHours()+":"+timeEat.getMinutes();
            timeNow = timeNow.getHours()+":"+timeNow.getMinutes();
            $.each(data.data[0].itemList,function(k,v){

                cpList+=' <p class="dish">'+v.name+'('+v.count+')</p>';

            })
            var orderId = oId;
            var tokenId=[];
            $.each(dataPort.tokenList,function(k,v){
                tokenId.push(v.id);
            })
            OrderingSystem.caluAmount(orderId, tokenId, function (data){
                fun();
                var discountMoney=data.data[0].caluAmount?(data.data[0].caluAmount/100):"0";
                $('    <ul class="cont">\
                <li class="tableInfo">\
                <p class="table">桌号</p>\
                <p class="info">'+dataPort.targetLabel+'</p>\
                </li>\
                    <li class="timeInfo">\
                        <p class="time">时间</p>\
                        <p class="info">'+timeEat+'-'+timeNow+'</p>\
                    </li>\
                    <div class="orderAndPay">\
                        <div class="billInfo" data-function="todayOrdering_billed_info" data-id="'+orderId+'">\
                            <p class="bill" data-function="todayOrdering_billed_info" data-id="'+orderId+'">账单</p>\
                            <div class="yeah" data-function="todayOrdering_billed_info" data-id="'+orderId+'">'+cpList+'\
                            </div data-function="todayOrdering_billed_info" data-id="'+orderId+'">\
                            <p class="goToSee" data-function="todayOrdering_billed_info" data-id="'+orderId+'">〉</p>\
                        </div>\
                        <li class="sumInfo">\
                            <p class="sum">共计</p>\
                            <p class="money">￥'+dataPort.total/100+'（预付￥0）</p>\
                        </li>\
                    </div>\
                    <!-- 应收-->\
                    <li class="receivableInfo">\
                        <p class="receivable">应收</p>\
                        <p class="money">￥'+discountMoney+'</p>\
                    </li>\
                    <li class="couponInfo">\
                        <p class="coupon active">未使用优惠券</p>\
                        <div class="chooseCouponBtn active"  data-function="btn_orderingFood_coupon_list_down"></div>\
                        <div class="showCouponOption hiddenCouponOption" style="display: none">\
                        </div>\
                    </li>\
                    <li class="payWayInfo">\
                        <p class="payWay">支付方式</p>\
                        <ul class="wayInfo">\
                            <li class="payMoney ">\
                                现金支付\
                                <div class="state active" >\
                                    <p class="noChoose choosed" data-function="btn_orderingFood_payWay" data-key="cash">√</p>\
                                </div>\
                            </li>\
                            <li class="payMoney" style="display:none">\
                                扫码支付\
                                <div class="state ">\
                                    <p class="noChoose choosed" data-function="btn_orderingFood_payWay_saoma" data-key="">√</p>\
                                </div>\
                            </li>\
                            <li class="payMoney">\
                                划卡支付\
                                <div class="state ">\
                                    <p class="noChoose choosed " data-function="btn_orderingFood_payWay" data-key="pos">√</p>\
                                </div>\
                            </li>\
                            <li class="payMoney">\
                                会员支付\
                                <div class="state ">\
                                    <p class="noChoose choosed" data-function="btn_orderingFood_payWay" data-id="'+dataPort.id+'" data-key="member">√</p>\
                                </div>\
                            </li>\
                        </ul>\
                    </li>\
                </ul>\
                <div class="bottom">\
                <div class="gathering active" data-function="btn_orderingFood_billGetMoney" data-id="'+dataPort.id+'">收款</div>\
                </div>').appendTo('#subchoosePayWayPage');
                var options = {};
                options.successFunction = function (data) {
                    $(' <p class="option without" data-function="btn_orderingFood_one_coupon" data-id="" data-orderid="'+orderId+'">不使用优惠券</p>').appendTo('#subchoosePayWayPage .showCouponOption');
                    $.each(data.data,function(k,v){
                        $(' <p class="option without" data-function="btn_orderingFood_one_coupon" data-id="'+v.id+'" data-orderid="'+orderId+'">'+v.name+'</p>').appendTo('#subchoosePayWayPage .showCouponOption');
                    })
                };
                OrderingSystem.getActiveToken(options);
            })


        });


    };
    /*
     * 点击一个优惠券
     * */
    var clickOneCoupon = function(obj){
        var billPage= $('#subchoosePayWayPage');
        if(obj.data('id')){
            COUPON[0]=obj.data('id');
        }
        if(obj.data('id')){
            var orderId = obj.data('orderid');
            var tokenId = [obj.data('id')];
            OrderingSystem.caluAmount(orderId, tokenId, function (data) {
                $('.receivableInfo .money',billPage).html('￥'+data.data[0].caluAmount/100);
            });
        }else{
            var oId =  obj.data('orderid');
            OrderingSystem.getTotal(oId, function (data) {
                $('.receivableInfo .money',billPage).html('￥'+data.data[0].total/100);
            });
        }
        var content = obj.html();
        $('#subchoosePayWayPage .couponInfo .coupon').html(content);
        obj.parent().hide();
    }
    /*
     * 选择一个支付方式
     * */
    var chooseWhichWay = function(obj,fun){
        PAYWAY=obj.data('key');
        var orderId = obj.data('id');
        var tokenId =COUPON;
        obj.parents('.wayInfo').find('.active').removeClass('active');
        obj.parent().addClass('active');
        if(PAYWAY == "member"){
            ORDERID = orderId;
            TOKENID = tokenId;
            OrderingSystem.caluAmount(orderId, tokenId, function (dataM) {
                MONEYMEMBER = dataM.data[0].caluAmount?(dataM.data[0].caluAmount):"0";
                OrderingSystem.getMemberList(function (data) {
                    fun();
                    $('#subVipMainPage .searchText').val('');
                    $('#subVipMainPage .vipList').html('');
                    $.each(data.data, function (k, v) {
                        $('<li class="vipInfo" data-id="'+ v.id+'" data-function="btn_orderingFood_chooseMember">\
                            <div class="vipIco" data-function="btn_orderingFood_chooseMember"></div>\
                            <p class="info" data-function="btn_orderingFood_chooseMember">'+v.memberName+'，电话：'+v.memberMobile+'</p>\
                        </li>').appendTo('#subVipMainPage .vipList');
                    });
                    //为返回按钮和跳页，初始化页面选项
                    obj.parents('.wayInfo').find('.active').removeClass('active');
                    $('#subchoosePayWayPage .wayInfo').children(":first").find("div").addClass("active");
                    PAYWAY='cash';//初始化支付类型
                })
            })
        }
    }
    /*
     * 扫码支付
     * */
    var saomaPay = function(obj,fun){
        PAYWAY=obj.data('key');
        obj.parents('.wayInfo').find('.active').removeClass('active');
        obj.parent().addClass('active');
        fun();
    }
    /*
     * 确认划菜按钮
     * */
    var elideCpOk = function(){
        $('#subcutDishesPage .IndexPage').show();
        $('#subcutDishesPage .cutOrNo').show();

    }
    /*
     * 是否划菜，否
     * */
    var elideCpIsOrNoNo = function(){
        $('#subcutDishesPage .IndexPage').hide();
        $('#subcutDishesPage .cutOrNo').hide();
    }
    /*
     * 是否划菜，是
     * */
    var elideCpIsOrNoYes = function(obj,fun){
        $('#subcutDishesPage .IndexPage').hide();
        $('#subcutDishesPage .cutOrNo').hide();
        var ulList=$('#subcutDishesPage .cont .active');
        $(ulList).each(function(k,v){

            var options = {};
            options.lineItemId =  $(v).data('id');
            options.successFunction = function (data) {

            };
            OrderingSystem.confirmationCpReachTable(options);
            //alert($(v).data('id'));
        })
        fun();
    };
    /*
     * 退掉菜品确定
     * */
    var deleteCpOk= function(obj,fun){
        var oId = obj.data('id');
        var reason = '';
        var parentNode = $('#subdelDishesPage');
        $('.reasonOptions .active',parentNode).each(function(){
            reason+=$(this).html();
        });
        reason+=$('.reasonText',parentNode).val();
        if(reason){
            $('.dishInfo',parentNode).each(function(){
                if($(this).find('.chooseIco').hasClass('active')){
                    if($(this).find('.count').html().replace("份","")*1 >0){
                        var options = {};
                        options.itemId = $(this).data('id');
                        var count = $(this).find('.count').html().replace("份","")*1;
                        options.count = count*(-1);
                        options.wrongReason = reason;
                        options.successFunction = function (data){
                            showOccupyTable(oId,fun);
                        };
                        OrderingSystem.cancelLineItem(options);
                    }
                }
            });
        }else{
            alert('请选择删除菜品理由')
            $(".wait_gear").fadeOut(100);
        }
    }
    /*
     * 菜品免单确定
     * */
    var cpFreeOk = function(obj,fun){
        var reason = '';
        var parentNode = $('#subfreeBillPage');
        $('.reasonOptions .active',parentNode).each(function(){
            reason+=$(this).html();
        });
        reason+=$('.reasonText',parentNode).val();
        if(reason){
            $('.dishInfo',parentNode).each(function(){
                if($(this).hasClass('active')){
                    var options = {};
                    options.lineItemId = $(this).data('id');
                    options.Reason = reason;
                    options.freeState = '1';
                    options.successFunction = function (data) {
                        fun();
                    };
                    OrderingSystem.setCpisFree(options);

                }
            });
        }else{

            alert('请选择免单理由')
            $(".wait_gear").fadeOut(100);
        }



    }
    /*
     *结账的收款按钮
     * */
    var biilGetMoney = function(obj,fun){
        var oId = obj.data('id');
        var token = COUPON;
        var memberId = '';
        var orderId = obj.data('id');
        var tokenId =COUPON;
        OrderingSystem.caluAmount(orderId, tokenId, function (data) {
            var discountMoney=data.data[0].caluAmount?(data.data[0].caluAmount):"0";
            OrderingSystem.addOrderPayTrans(orderId,discountMoney, PAYWAY,function(){
                OrderingSystem.bill(oId, token, memberId,"",function (data) {
                    fun();
                    PAYWAY='cash';
                    COUPON=[];
                    OrderingSystem.billOrderPrinter(oId, function (data) {});
                });
            });
        });
    }

    /*
     * 会员结账选择会员
     * */
    var chooseMember = function(obj,fun){
        if(obj.parent('li').data('id')){
            MEMBERID = obj.closest('li').data('id');
        }
        else{
            MEMBERID = obj.data('id');
        }
        var mId = MEMBERID;
        $('#subVipPayPage .IndexPage').hide();
        $('#subVipPayPage .overIndexPage').hide();
        $('#subVipPayPage .subVipPayPageCont').html('');
        OrderingSystem.getMemberById(mId,function(data){
            OrderingSystem.getMemberAccountAmount(mId,function(dataAA){
                OrderingSystem.getRestaurant(function(dataR){
                    /*var memberLevel = ["","一","二","三","四","五","六","七","八","九","十"];*/
                    var status = "";
                    if(data.data[0].status == "active"){
                        status = "激活";
                        $('#subVipPayPage .payBtn').removeClass("active");
                    }
                    if(data.data[0].status == "deactive"){
                        status = "冻结";
                        $('#subVipPayPage .payBtn').addClass("active");
                    }
                    $('<p class="expendMoney">'+dataR.data[0].RName+'&nbsp餐费：￥'+(MONEYMEMBER/100).toFixed(2)+'</p>\
                        <ul class="vipList"></ul>').appendTo('#subVipPayPage .subVipPayPageCont');
                    $('<li class="vipInfoTitle">个人信息</li>\
                           <li class="vipInfo">\
                               <p class="vipInfoHead">姓名：</p>\
                               <p class="vipInfoCont">'+data.data[0].memberName+'</p>\
                           </li>\
                           <li class="vipInfo">\
                               <p class="vipInfoHead">电话：</p>\
                               <p class="vipInfoCont">'+data.data[0].memberMobile+'</p>\
                           </li>\
                           <li class="vipInfo">\
                               <p class="vipInfoHead">会员状态：</p>\
                               <p class="vipInfoCont">'+status+'</p>\
                           </li>\
                           <li class="vipInfo">\
                               <p class="vipInfoHead">会员等级：</p>\
                               <p class="vipInfoCont">'+data.data[0].levelName+'</p>\
                           </li>\
                       <li class="restMoney">余额：'+(dataAA.data[0].point/100).toFixed(2)+'￥</li>').appendTo('#subVipPayPage .vipList');
                    fun();
                })
            })
        })
    }
    /*
     * 会员支付页面点击结账
     * */
    var biilGetMoneyMember = function(fun){
        var orderId = ORDERID;
        var tokenId = TOKENID;
        var memberId = MEMBERID;
        OrderingSystem.getMemberById(memberId,function(dataM){
            OrderingSystem.caluAmount(orderId, tokenId, function (data) {
                var options = {};
                options.mId = memberId;
                options.oId = orderId;
                options.money = data.data[0].caluAmount?(data.data[0].caluAmount):"0";
                options.memo = dataM.data[0].memberMemo;
                if(options.money <= dataM.data[0].residuePoint){
                    options.successFunction = function() {
                        OrderingSystem.bill(orderId,tokenId,memberId,"", function (data) {
                            OrderingSystem.billOrderPrinter(orderId, function (data) {
                                fun();
                            })
                        });
                    };
                    OrderingSystem.memberSpend(options);
                }
                else{
                    $('#subVipPayPage .IndexPage').show();
                    $('#subVipPayPage .overIndexPage').show();
                    $('.wait_gear').hide();//关闭等待页面
                }
            });
        })
    };
    var chooseOtherPayWay = function(fun){
        //隐藏弹窗
        $('#subVipPayPage .IndexPage').hide();
        $('#subVipPayPage .overIndexPage').hide();
        /*//初始化选择支付方式页面
         $('#subchoosePayWayPage .state').removeClass("active");
         $('#subchoosePayWayPage .wayInfo').children(":first").find("div").addClass("active");
         PAYWAY='cash';//初始化支付类型*/
        fun();
    };
    /*
     * 占用桌结账的订单信息
     * */
    var billTableCpInfo = function(obj,fun){
        var parentsNode = $('#subBillDetailsPage');
        var parensNodeOfBilled = $('#subchoosePayWayPage');
        var payReceivable = $('.receivableInfo .money',parensNodeOfBilled).html();
        var oId = obj.data('id');
        OrderingSystem.getLineItemByOIdList(oId, function (data) {
            $('.noPayList',parentsNode).html("");
            $.each(data.data,function(k,v){
                $('<li class="noPay">\
                    <p class="name">'+v.name+'</p>\
                    <p class="count">'+v.count+'份</p>\
                    <p class="price">￥'+v.price/100+'</p>\
                </li>').appendTo('#subBillDetailsPage .noPayList');
            })
            $('  <li class="sumInfo">\
                <p class="sum">应收：</p>\
                <p class="money">'+payReceivable+'</p>\
            </li>').appendTo('#subBillDetailsPage .noPayList');
            fun();

        });

    }
    /************就餐开台的流程结束*******************/
    return{
        /**占用桌的流程开始**/
        occupyFlowStart:function(obj,fun){
            occupyFlowStart(obj,fun);
        },
        /*占用桌页面的菜品详情*/
        occupyTableCpInfo:function(obj,fun){
            occupyTableCpInfo(obj,fun);
        },
        /*占用桌换桌*/
        occupyTableExchangeTable:function(obj,fun){
            occupyTableExchangeTable(obj,fun)
        },
        /*占用桌换台，点击桌台列表*/
        occupyTableChangeTableClickTableList:function(obj,fun){
            occupyTableChangeTableClickTableList(obj,fun)
        },
        /*占用桌加菜*/
        occupyTableAddCp:function(obj,fun){
            occupyTableAddCp(obj,fun)
        },
        /*占用桌划菜*/
        occupyTableElideCp:function(obj,fun){
            occupyTableElideCp(obj,fun);
        },
        occupyTableDeleteCp:function(obj,fun){
            occupyTableDeleteCp(obj,fun);
        },
        occupyTableFreeCp:function(obj,fun){
            occupyTableFreeCp(obj,fun);
        },
        /*占用桌结账*/
        occupyTableBillOrder:function(obj,fun){
            occupyTableBillOrder(obj,fun)
        },
        /* 确认划菜按钮 */
        elideCpOk:function(){
            elideCpOk();
        },
        /*是否划菜，否*/
        elideCpIsOrNoNo:function(){
            elideCpIsOrNoNo()
        },
        /*是否划菜，是*/
        elideCpIsOrNoYes:function(obj,fun){
            elideCpIsOrNoYes(obj,fun);
        },
        /*删除菜品确定按钮*/
        deleteCpOk:function(obj,fun){
            deleteCpOk(obj,fun)
        },
        /*菜品免单确定按钮*/
        cpFreeOk:function(obj,fun){
            cpFreeOk(obj,fun);
        },
        /*退菜页面的全选按钮*/
        allChoose:function(obj){
            allChoose(obj);
        },
        /*退菜页面的每道菜的选择按钮*/
        oneChoose:function(obj){
            oneChoose(obj);
        },
        /*退菜页面中的减号按钮*/
        subtractCpCount:function(obj){
            subtractCpCount(obj);
        },
        /*退菜页面中的加号按钮*/
        addCpCount:function(obj){
            addCpCount(obj);
        },
        /*结账收款*/
        biilGetMoney:function(obj,fun){
            biilGetMoney(obj,fun);
        },
        /*会员支付页面点击结账按钮*/
        biilGetMoneyMember:function(fun){
            biilGetMoneyMember(fun);
        },
        /*会员余额不足，选择其他支付方式*/
        chooseOtherPayWay:function(fun){
            chooseOtherPayWay(fun);
        },
        /*点击一个优惠券*/
        clickOneCoupon:function(obj){
            clickOneCoupon(obj);
        },
        /*选择哪个支付方式*/
        chooseWhichWay:function(obj,fun){
            chooseWhichWay(obj,fun);
        },
        /*扫码支付*/
        saomaPay:function(obj,fun){
            saomaPay(obj,fun);
        },
        /*会员支付选择会员*/
        chooseMember:function(obj,fun){
            chooseMember(obj,fun);
        },
        /*
         * 占用桌的订单详情
         * */
        billTableCpInfo:function(obj,fun){
            billTableCpInfo(obj,fun);
        }
    }
}();